﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using PS.DBUtility;
namespace PS.DAL
{
    /// <summary>
    /// 数据访问类:RepResourceLoan
    /// </summary>
    public partial class RepResourceLoan
    {
        public RepResourceLoan()
        { }
        #region  Method
        /// <summary>
        /// 是否存在该记录

        /// </summary>
        public bool Exists(int RepResourceLoanID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from RepResourceLoan");
            strSql.Append(" where RepResourceLoanID=@RepResourceLoanID");
            SqlParameter[] parameters = {
					new SqlParameter("@RepResourceLoanID", SqlDbType.Int,4)
            };
            parameters[0].Value = RepResourceLoanID;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        //GetSingle

            public string GetTodayLoanNum()
            {
                 StringBuilder strSql = new StringBuilder();
            //strSql.Append("select count(1) from RepResourceLoan "); 
                strSql.Append(" declare @result varchar(50) ");
                strSql.Append(" select @result= (select top 1 LoanNumber  from RepResourceLoan ");
                strSql.Append("where cast(  LoanNumber as varchar(8))=cast(replace(replace(replace(CONVERT(varchar,  getdate()  , 120 ),'-',''),' ',''),':','') as varchar(8))");
                strSql.Append("order by LoanNumber desc)  "); 
                strSql.Append(" select  case when(isnull(@result,'')='') then ");
                strSql.Append(" cast(replace(replace(replace(CONVERT(varchar,  getdate()  , 120 ),'-',''),' ',''),':','') as varchar(8))");
                strSql.Append(" +'0001' else cast( @result as bigint)+1 end ");
                object obj = DbHelperSQL.GetSingle(strSql.ToString());
                if (obj == null)
                {
                    return "";
                }
                else
                {
                    return obj.ToString();
                }

            }

        /// <summary>
        /// 增加一条数据

        /// </summary>
        public int Add(PS.Model.RepResourceLoan model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into RepResourceLoan(");
            strSql.Append("RepResourceMangID,UseDepID,UseLocation,UsePurpose,RelationCall,LoanDateTime,Operator,ApplyUser,BillingDateTime,PlanRevertDate,RevertDate,ResourceStatus,Driver,Editor,EditTime,ValidMark,Spare1,Spare2,Spare3,Spare4,Reverter,LoanNumber)");
            strSql.Append(" values (");
            strSql.Append("@RepResourceMangID,@UseDepID,@UseLocation,@UsePurpose,@RelationCall,@LoanDateTime,@Operator,@ApplyUser,@BillingDateTime,@PlanRevertDate,@RevertDate,@ResourceStatus,@Driver,@Editor,@EditTime,@ValidMark,@Spare1,@Spare2,@Spare3,@Spare4,@Reverter,@LoanNumber)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@RepResourceMangID", SqlDbType.Int,4),
					new SqlParameter("@UseDepID", SqlDbType.Int,4),
					new SqlParameter("@UseLocation", SqlDbType.VarChar,400),
					new SqlParameter("@UsePurpose", SqlDbType.VarChar,400),
					new SqlParameter("@RelationCall", SqlDbType.VarChar,30),
					new SqlParameter("@LoanDateTime", SqlDbType.DateTime),
					new SqlParameter("@Operator", SqlDbType.Int,4),
					new SqlParameter("@ApplyUser", SqlDbType.VarChar,100),
					new SqlParameter("@BillingDateTime", SqlDbType.DateTime),
					new SqlParameter("@PlanRevertDate", SqlDbType.DateTime),
					new SqlParameter("@RevertDate", SqlDbType.DateTime),
					new SqlParameter("@ResourceStatus", SqlDbType.VarChar,20),
					new SqlParameter("@Driver", SqlDbType.VarChar,100),
					new SqlParameter("@Editor", SqlDbType.Int,4),
					new SqlParameter("@EditTime", SqlDbType.DateTime),
					new SqlParameter("@ValidMark", SqlDbType.Int,4),
					new SqlParameter("@Spare1", SqlDbType.VarChar,200),
					new SqlParameter("@Spare2", SqlDbType.VarChar,200),
					new SqlParameter("@Spare3", SqlDbType.VarChar,200),
					new SqlParameter("@Spare4", SqlDbType.VarChar,200),
					new SqlParameter("@Reverter", SqlDbType.Int,4),
                    new SqlParameter("@LoanNumber", SqlDbType.VarChar,200)};
            parameters[0].Value = model.RepResourceMangID;
            parameters[1].Value = model.UseDepID;
            parameters[2].Value = model.UseLocation;
            parameters[3].Value = model.UsePurpose;
            parameters[4].Value = model.RelationCall;
            parameters[5].Value = model.LoanDateTime;
            parameters[6].Value = model.Operator;
            parameters[7].Value = model.ApplyUser;
            parameters[8].Value = model.BillingDateTime;
            parameters[9].Value = model.PlanRevertDate;
            parameters[10].Value = model.RevertDate;
            parameters[11].Value = model.ResourceStatus;
            parameters[12].Value = model.Driver;
            parameters[13].Value = model.Editor;
            parameters[14].Value = model.EditTime;
            parameters[15].Value = model.ValidMark;
            parameters[16].Value = model.Spare1;
            parameters[17].Value = model.Spare2;
            parameters[18].Value = model.Spare3;
            parameters[19].Value = model.Spare4;
            parameters[20].Value = model.Reverter;
            parameters[21].Value = model.LoanNumber;
            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据

        /// </summary>
        public bool Update(PS.Model.RepResourceLoan model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update RepResourceLoan set ");
            strSql.Append("RepResourceMangID=@RepResourceMangID,");
            strSql.Append("UseDepID=@UseDepID,");
            strSql.Append("UseLocation=@UseLocation,");
            strSql.Append("UsePurpose=@UsePurpose,");
            strSql.Append("RelationCall=@RelationCall,");
            strSql.Append("LoanDateTime=@LoanDateTime,");
            strSql.Append("Operator=@Operator,");
            strSql.Append("ApplyUser=@ApplyUser,");
            strSql.Append("BillingDateTime=@BillingDateTime,");
            strSql.Append("PlanRevertDate=@PlanRevertDate,");
            strSql.Append("RevertDate=@RevertDate,");
            strSql.Append("ResourceStatus=@ResourceStatus,");
            strSql.Append("Driver=@Driver,");
            strSql.Append("Editor=@Editor,");
            strSql.Append("EditTime=@EditTime,");
            strSql.Append("ValidMark=@ValidMark,");
            strSql.Append("Spare1=@Spare1,");
            strSql.Append("Spare2=@Spare2,");
            strSql.Append("Spare3=@Spare3,");
            strSql.Append("Spare4=@Spare4, ");
            strSql.Append("Reverter=@Reverter, ");
            strSql.Append("LoanNumber=@LoanNumber ");
            strSql.Append(" where RepResourceLoanID=@RepResourceLoanID");
            SqlParameter[] parameters = {
					new SqlParameter("@RepResourceMangID", SqlDbType.Int,4),
					new SqlParameter("@UseDepID", SqlDbType.Int,4),
					new SqlParameter("@UseLocation", SqlDbType.VarChar,400),
					new SqlParameter("@UsePurpose", SqlDbType.VarChar,400),
					new SqlParameter("@RelationCall", SqlDbType.VarChar,30),
					new SqlParameter("@LoanDateTime", SqlDbType.DateTime),
					new SqlParameter("@Operator", SqlDbType.Int,4),
					new SqlParameter("@ApplyUser", SqlDbType.VarChar,100),
					new SqlParameter("@BillingDateTime", SqlDbType.DateTime),
					new SqlParameter("@PlanRevertDate", SqlDbType.DateTime),
					new SqlParameter("@RevertDate", SqlDbType.DateTime),
					new SqlParameter("@ResourceStatus", SqlDbType.VarChar,20),
					new SqlParameter("@Driver", SqlDbType.VarChar,100),
					new SqlParameter("@Editor", SqlDbType.Int,4),
					new SqlParameter("@EditTime", SqlDbType.DateTime),
					new SqlParameter("@ValidMark", SqlDbType.Int,4),
					new SqlParameter("@Spare1", SqlDbType.VarChar,200),
					new SqlParameter("@Spare2", SqlDbType.VarChar,200),
					new SqlParameter("@Spare3", SqlDbType.VarChar,200),
					new SqlParameter("@Spare4", SqlDbType.VarChar,200),
					new SqlParameter("@Reverter", SqlDbType.Int,4),
					new SqlParameter("@LoanNumber", SqlDbType.VarChar,200),
					new SqlParameter("@RepResourceLoanID", SqlDbType.Int,4)};
            parameters[0].Value = model.RepResourceMangID;
            parameters[1].Value = model.UseDepID;
            parameters[2].Value = model.UseLocation;
            parameters[3].Value = model.UsePurpose;
            parameters[4].Value = model.RelationCall;
            parameters[5].Value = model.LoanDateTime;
            parameters[6].Value = model.Operator;
            parameters[7].Value = model.ApplyUser;
            parameters[8].Value = model.BillingDateTime;
            parameters[9].Value = model.PlanRevertDate;
            parameters[10].Value = model.RevertDate;
            parameters[11].Value = model.ResourceStatus;
            parameters[12].Value = model.Driver;
            parameters[13].Value = model.Editor;
            parameters[14].Value = model.EditTime;
            parameters[15].Value = model.ValidMark;
            parameters[16].Value = model.Spare1;
            parameters[17].Value = model.Spare2;
            parameters[18].Value = model.Spare3;
            parameters[19].Value = model.Spare4;
            parameters[20].Value = model.Reverter;
            parameters[21].Value = model.LoanNumber;

            parameters[22].Value = model.RepResourceLoanID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据

        /// </summary>
        public bool Delete(int RepResourceLoanID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from RepResourceLoan ");
            strSql.Append(" where RepResourceLoanID=@RepResourceLoanID");
            SqlParameter[] parameters = {
					new SqlParameter("@RepResourceLoanID", SqlDbType.Int,4)
};
            parameters[0].Value = RepResourceLoanID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 删除一条数据

        /// </summary>
        public bool DeleteList(string RepResourceLoanIDlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from RepResourceLoan ");
            strSql.Append(" where RepResourceLoanID in (" + RepResourceLoanIDlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体

        /// </summary>
        public PS.Model.RepResourceLoan GetModel(int RepResourceLoanID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 RepResourceLoanID,RepResourceMangID,UseDepID,UseLocation,UsePurpose,RelationCall,LoanDateTime,Operator,ApplyUser,BillingDateTime,PlanRevertDate,RevertDate,ResourceStatus,Driver,Editor,EditTime,ValidMark,Spare1,Spare2,Spare3,Spare4,Reverter,LoanNumber from RepResourceLoan ");
            strSql.Append(" where RepResourceLoanID=@RepResourceLoanID");
            SqlParameter[] parameters = {
					new SqlParameter("@RepResourceLoanID", SqlDbType.Int,4)
};
            parameters[0].Value = RepResourceLoanID;

            PS.Model.RepResourceLoan model = new PS.Model.RepResourceLoan();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["RepResourceLoanID"].ToString() != "")
                {
                    model.RepResourceLoanID = int.Parse(ds.Tables[0].Rows[0]["RepResourceLoanID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["RepResourceMangID"].ToString() != "")
                {
                    model.RepResourceMangID = int.Parse(ds.Tables[0].Rows[0]["RepResourceMangID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["UseDepID"].ToString() != "")
                {
                    model.UseDepID = int.Parse(ds.Tables[0].Rows[0]["UseDepID"].ToString());
                }
                model.UseLocation = ds.Tables[0].Rows[0]["UseLocation"].ToString();
                model.UsePurpose = ds.Tables[0].Rows[0]["UsePurpose"].ToString();
                model.RelationCall = ds.Tables[0].Rows[0]["RelationCall"].ToString();
                if (ds.Tables[0].Rows[0]["LoanDateTime"].ToString() != "")
                {
                    model.LoanDateTime = DateTime.Parse(ds.Tables[0].Rows[0]["LoanDateTime"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Operator"].ToString() != "")
                {
                    model.Operator = int.Parse(ds.Tables[0].Rows[0]["Operator"].ToString());
                }
                model.ApplyUser = ds.Tables[0].Rows[0]["ApplyUser"].ToString();
                if (ds.Tables[0].Rows[0]["BillingDateTime"].ToString() != "")
                {
                    model.BillingDateTime = DateTime.Parse(ds.Tables[0].Rows[0]["BillingDateTime"].ToString());
                }
                if (ds.Tables[0].Rows[0]["PlanRevertDate"].ToString() != "")
                {
                    model.PlanRevertDate = DateTime.Parse(ds.Tables[0].Rows[0]["PlanRevertDate"].ToString());
                }
                if (ds.Tables[0].Rows[0]["RevertDate"].ToString() != "")
                {
                    model.RevertDate = DateTime.Parse(ds.Tables[0].Rows[0]["RevertDate"].ToString());
                }
                model.ResourceStatus = ds.Tables[0].Rows[0]["ResourceStatus"].ToString();
                model.Driver = ds.Tables[0].Rows[0]["Driver"].ToString();
                if (ds.Tables[0].Rows[0]["Editor"].ToString() != "")
                {
                    model.Editor = int.Parse(ds.Tables[0].Rows[0]["Editor"].ToString());
                }
                if (ds.Tables[0].Rows[0]["EditTime"].ToString() != "")
                {
                    model.EditTime = DateTime.Parse(ds.Tables[0].Rows[0]["EditTime"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ValidMark"].ToString() != "")
                {
                    model.ValidMark = int.Parse(ds.Tables[0].Rows[0]["ValidMark"].ToString());
                }
                model.Spare1 = ds.Tables[0].Rows[0]["Spare1"].ToString();
                model.Spare2 = ds.Tables[0].Rows[0]["Spare2"].ToString();
                model.Spare3 = ds.Tables[0].Rows[0]["Spare3"].ToString();
                model.Spare4 = ds.Tables[0].Rows[0]["Spare4"].ToString();

                if (ds.Tables[0].Rows[0]["Reverter"].ToString() != "")
                {
                    model.Reverter = int.Parse(ds.Tables[0].Rows[0]["Reverter"].ToString());
                }
                model.LoanNumber = ds.Tables[0].Rows[0]["LoanNumber"].ToString();


                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 得到一个对象实体

        /// </summary>
        public PS.Model.RepResourceLoan GetModelByRepResourceMangID(int RepResourceMangID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 RepResourceLoanID,RepResourceMangID,UseDepID,UseLocation,UsePurpose,RelationCall,LoanDateTime,Operator,ApplyUser,BillingDateTime,PlanRevertDate,RevertDate,ResourceStatus,Driver,Editor,EditTime,ValidMark,Spare1,Spare2,Spare3,Spare4,Reverter,LoanNumber from RepResourceLoan ");
            strSql.Append(" where RepResourceMangID=@RepResourceMangID   order by RepResourceLoanID desc ");
            SqlParameter[] parameters = {
					new SqlParameter("@RepResourceMangID", SqlDbType.Int,4)
};
            parameters[0].Value = RepResourceMangID;

            PS.Model.RepResourceLoan model = new PS.Model.RepResourceLoan();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["RepResourceLoanID"].ToString() != "")
                {
                    model.RepResourceLoanID = int.Parse(ds.Tables[0].Rows[0]["RepResourceLoanID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["RepResourceMangID"].ToString() != "")
                {
                    model.RepResourceMangID = int.Parse(ds.Tables[0].Rows[0]["RepResourceMangID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["UseDepID"].ToString() != "")
                {
                    model.UseDepID = int.Parse(ds.Tables[0].Rows[0]["UseDepID"].ToString());
                }
                model.UseLocation = ds.Tables[0].Rows[0]["UseLocation"].ToString();
                model.UsePurpose = ds.Tables[0].Rows[0]["UsePurpose"].ToString();
                model.RelationCall = ds.Tables[0].Rows[0]["RelationCall"].ToString();
                if (ds.Tables[0].Rows[0]["LoanDateTime"].ToString() != "")
                {
                    model.LoanDateTime = DateTime.Parse(ds.Tables[0].Rows[0]["LoanDateTime"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Operator"].ToString() != "")
                {
                    model.Operator = int.Parse(ds.Tables[0].Rows[0]["Operator"].ToString());
                }
                model.ApplyUser = ds.Tables[0].Rows[0]["ApplyUser"].ToString();
                if (ds.Tables[0].Rows[0]["BillingDateTime"].ToString() != "")
                {
                    model.BillingDateTime = DateTime.Parse(ds.Tables[0].Rows[0]["BillingDateTime"].ToString());
                }
                if (ds.Tables[0].Rows[0]["PlanRevertDate"].ToString() != "")
                {
                    model.PlanRevertDate = DateTime.Parse(ds.Tables[0].Rows[0]["PlanRevertDate"].ToString());
                }
                if (ds.Tables[0].Rows[0]["RevertDate"].ToString() != "")
                {
                    model.RevertDate = DateTime.Parse(ds.Tables[0].Rows[0]["RevertDate"].ToString());
                }
                model.ResourceStatus = ds.Tables[0].Rows[0]["ResourceStatus"].ToString();
                model.Driver = ds.Tables[0].Rows[0]["Driver"].ToString();
                if (ds.Tables[0].Rows[0]["Editor"].ToString() != "")
                {
                    model.Editor = int.Parse(ds.Tables[0].Rows[0]["Editor"].ToString());
                }
                if (ds.Tables[0].Rows[0]["EditTime"].ToString() != "")
                {
                    model.EditTime = DateTime.Parse(ds.Tables[0].Rows[0]["EditTime"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ValidMark"].ToString() != "")
                {
                    model.ValidMark = int.Parse(ds.Tables[0].Rows[0]["ValidMark"].ToString());
                }
                model.Spare1 = ds.Tables[0].Rows[0]["Spare1"].ToString();
                model.Spare2 = ds.Tables[0].Rows[0]["Spare2"].ToString();
                model.Spare3 = ds.Tables[0].Rows[0]["Spare3"].ToString();
                model.Spare4 = ds.Tables[0].Rows[0]["Spare4"].ToString();

                if (ds.Tables[0].Rows[0]["Reverter"].ToString() != "")
                {
                    model.Reverter = int.Parse(ds.Tables[0].Rows[0]["Reverter"].ToString());
                }
                model.LoanNumber = ds.Tables[0].Rows[0]["LoanNumber"].ToString();

                return model;
            }
            else
            {
                return null;
            }
        }



        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select RepResourceLoanID,RepResourceMangID,UseDepID,UseLocation,UsePurpose,RelationCall,LoanDateTime,Operator,ApplyUser,BillingDateTime,PlanRevertDate,RevertDate,ResourceStatus,Driver,Editor,EditTime,ValidMark,Spare1,Spare2,Spare3,Spare4,Reverter,LoanNumber ");
            strSql.Append(" FROM RepResourceLoan ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得全部数据列表
        /// </summary>
        public DataSet GetAllList(string LoanNumber, string UseDepID, string ApplyUser, string ResourceName, string ResourceCode, DateTime? Date1, DateTime? Date2, string CurrentStatus,  
             int pageSize, int pageIndex, out long totalCount)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT     a.RepResourceLoanID, a.RepResourceMangID, a.UseDepID, a.UseLocation, a.UsePurpose, a.RelationCall, CONVERT(varchar(10), a.LoanDateTime, 120)  as LoanDateTime, a.Operator, a.ApplyUser, ");
            strSql.Append(" CONVERT(varchar(10), a.BillingDateTime, 120)  as BillingDateTime,  CONVERT(varchar(10), a.PlanRevertDate, 120)  as  PlanRevertDate,( CASE WHEN (ISNULL( a.RevertDate,'')='' ) THEN '未归还' ELSE CONVERT(varchar(10), a.RevertDate, 120)  END )as  RevertDate, ");
           strSql.Append(" a.ResourceStatus, a.Driver, a.Editor, a.EditTime, a.ValidMark, a.Spare1, a.Spare2, a.Spare3, a.Spare4, a.Reverter,  ");
            strSql.Append(" a.LoanNumber, b.ResourceCode, b.ResourceName, b.ResourceModel, b.ResourceType, b.Memo, b.CurrentStatus, ApplyUserTable.PersonName AS ApplyUserName,  ");
            strSql.Append(" edp.DepName, edp.DepCode, OperationTable.UserName AS OperatorName ");
            strSql.Append(" FROM dbo.RepResourceLoan AS a LEFT OUTER JOIN ");
            strSql.Append("  dbo.EmpDep AS edp ON a.UseDepID = edp.DepID LEFT OUTER JOIN ");
            strSql.Append(" dbo.EmpPersonBase AS ApplyUserTable ON a.ApplyUser = CONVERT(varchar(10), ApplyUserTable.PersonID) LEFT OUTER JOIN ");
            strSql.Append(" dbo.RepResourceMang AS b ON a.RepResourceMangID = b.RepResourceMangID LEFT OUTER JOIN ");
            strSql.Append(" dbo.SysUser AS OperationTable ON a.Operator = OperationTable.UserID "); 
            strSql.Append(" WHERE     (a.ValidMark = 1) ");
            
             
             if (!string.IsNullOrEmpty(LoanNumber))
             {
                 strSql.Append(" and LoanNumber  like '%").Append(LoanNumber + "%' ");
             }
            //部门
             if (!string.IsNullOrEmpty(UseDepID))
             {
                 strSql.Append(" and a.UseDepID  = '").Append(UseDepID + "'"); 
             } 
            //申请人
             if (!string.IsNullOrEmpty(ApplyUser))
             {
                 strSql.Append(" and a.ApplyUser  = '").Append(ApplyUser + "'");
             }

             if (!string.IsNullOrEmpty(ResourceName))
             {
                 strSql.Append(" and ResourceName  like '%").Append(ResourceName + "%' ");
             }
             if (!string.IsNullOrEmpty(ResourceCode))
             {
                 strSql.Append(" and ResourceCode  like '%").Append(ResourceCode + "%' ");
             }
             //if (!string.IsNullOrEmpty(CurrentStatus))
             //{
             //    strSql.Append(" and CurrentStatus  = '").Append(CurrentStatus + "' ");
             //}
             if (Date1.HasValue)
             {
                 strSql.Append(" and a.LoanDateTime  >= '").Append(Date1 + "' ");
             }
             if (Date2.HasValue)
             {
                 strSql.Append(" and a.LoanDateTime  < '").Append(Date2.Value. AddDays(1) + "' ");
             }

             if (!string.IsNullOrEmpty(CurrentStatus))
             {
                 if (CurrentStatus=="未归还")
                     strSql.Append(" and ISNULL(a.RevertDate,'')=''  ");
                 else
                     strSql.Append(" and ISNULL(a.RevertDate,'')!=''  ");
             }


             strSql.Append("  order by a.RepResourceLoanID desc ");


             string totalSql = PageSplitHelper.GetTotalSql(strSql.ToString());
             totalCount = long.Parse(DbHelperSQL.ExecuteScalar(totalSql).ToString());

             string pageSql = PageSplitHelper.GetPageSql(strSql.ToString(), pageSize, pageIndex, totalCount);

             return DbHelperSQL.Query(pageSql);
 
            //return DbHelperSQL.Query(strSql.ToString() );
        }
 
        /// <summary>
        /// 获得前几行数据

        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" RepResourceLoanID,RepResourceMangID,UseDepID,UseLocation,UsePurpose,RelationCall,LoanDateTime,Operator,ApplyUser,BillingDateTime,PlanRevertDate,RevertDate,ResourceStatus,Driver,Editor,EditTime,ValidMark,Spare1,Spare2,Spare3,Spare4 ");
            strSql.Append(" FROM RepResourceLoan ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        public DataSet GetOneLoanByResourceMangID(int ResourceMangID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(" SELECT   TOP (1) a.RepResourceLoanID, a.RepResourceMangID, a.UseDepID, a.UseLocation, a.UsePurpose, a.RelationCall, "
             + "    a.LoanDateTime, a.Operator, a.ApplyUser, a.BillingDateTime, a.PlanRevertDate, a.RevertDate, a.ResourceStatus, "
             + "   a.Driver, a.Editor, a.EditTime, a.ValidMark, a.Spare1, a.Spare2, a.Spare3, a.Spare4,a.LoanNumber,a.Reverter, b.ResourceName, b.ResourceCode, EditorTable.UserName AS EditorName , "
             + "  b.ResourceModel, b.ResourceType, b.CurrentStatus, b.Memo,ApplyUserTable.PersonName AS ApplyUserName,edp.DepName,sds.keyvalue as resourcetypename , OperationTable.UserName AS OperatorName ,  ReverterTable.PersonName AS ReverterName"
             + "  FROM    dbo.RepResourceLoan AS a LEFT OUTER JOIN dbo.EmpPersonBase AS ApplyUserTable ON a.ApplyUser = CONVERT(varchar(10), ApplyUserTable.PersonID) LEFT OUTER JOIN "
             + "  dbo.EmpDep AS edp ON a.UseDepID = edp.DepID LEFT OUTER JOIN dbo.RepResourceMang AS b ON a.RepResourceMangID = b.RepResourceMangID  left join dbo.SysDictionarySub as sds  on b.resourcetype=sds.id and sds.mainkeyid=14 ");
            strSql.Append("  LEFT OUTER JOIN  dbo.SysUser AS OperationTable ON a.Operator = OperationTable.UserID  LEFT OUTER JOIN  dbo.SysUser AS EditorTable ON a.Editor = EditorTable.UserID  LEFT OUTER JOIN  dbo.EmpPersonBase AS ReverterTable ON a.Reverter=ReverterTable.PersonID  "); 
            strSql.Append(" where a.ValidMark=1 and a.RepResourceMangID=@RepResourceMangID  and Isnull(RevertDate,'')=''  order by RepResourceLoanID desc ");
            SqlParameter[] parameters = {
					new SqlParameter("@RepResourceMangID", SqlDbType.Int,4)
};
            parameters[0].Value = ResourceMangID;
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            return ds;
        }
            

        public DataSet GetOneLoanByResourceLoanID(int RepResourceLoanID)
        {


//            StringBuilder strSql = new StringBuilder();
//            strSql.Append(" SELECT   TOP (1) a.RepResourceLoanID, a.RepResourceMangID, a.UseDepID, a.UseLocation, a.UsePurpose, a.RelationCall, "
//             + "    a.LoanDateTime, a.Operator, a.ApplyUser, a.BillingDateTime, a.PlanRevertDate, a.RevertDate, a.ResourceStatus, "
//             + "   a.Driver, a.Editor, a.EditTime, a.ValidMark, a.Spare1, a.Spare2, a.Spare3, a.Spare4,a.LoanNumber,a.Reverter, b.ResourceName, b.ResourceCode,  EditorTable.UserName AS EditorName ,"
//             + "  b.ResourceModel, b.ResourceType, b.CurrentStatus, b.Memo,ApplyUserTable.PersonName AS ApplyUserName,edp.DepName ,sds.keyvalue as resourcetypename , OperationTable.UserName AS OperatorName,  ReverterTable.PersonName AS ReverterName "
//             + "  FROM    dbo.RepResourceLoan AS a LEFT OUTER JOIN dbo.EmpPersonBase AS ApplyUserTable ON a.ApplyUser = CONVERT(varchar(10), ApplyUserTable.PersonID) LEFT OUTER JOIN "
//             + "  dbo.EmpDep AS edp ON a.UseDepID = edp.DepID LEFT OUTER JOIN dbo.RepResourceMang AS b ON a.RepResourceMangID = b.RepResourceMangID   left join dbo.SysDictionarySub as sds  on b.resourcetype=sds.id and sds.mainkeyid=14 ") ;
//            strSql.Append("  LEFT OUTER JOIN  dbo.SysUser AS OperationTable ON a.Operator = OperationTable.UserID   LEFT OUTER JOIN  dbo.SysUser AS EditorTable ON a.Editor = EditorTable.UserID   LEFT OUTER JOIN  dbo.EmpPersonBase AS ReverterTable ON a.Reverter=ReverterTable.PersonID "); 
//            strSql.Append(" where a.ValidMark=1 and a.RepResourceLoanID=@RepResourceLoanID  and Isnull(RevertDate,'')=''  order by RepResourceLoanID desc ");
//            SqlParameter[] parameters = {
//                    new SqlParameter("@RepResourceLoanID", SqlDbType.Int,4)
//};
//            parameters[0].Value = RepResourceLoanID;
//            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            return GetOneLoanByResourceLoanID(RepResourceLoanID,"");
        }

        public DataSet GetOneLoanByResourceLoanIDAndRevert(int RepResourceLoanID)
        {
            return GetOneLoanByResourceLoanID(RepResourceLoanID,"  and Isnull(RevertDate,'')=''  ");
        } 

        public DataSet GetOneLoanByResourceLoanID(int RepResourceLoanID,string strCondtion)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(" SELECT   TOP (1) a.RepResourceLoanID, a.RepResourceMangID, a.UseDepID, a.UseLocation, a.UsePurpose, a.RelationCall, "
             + "    a.LoanDateTime, a.Operator, a.ApplyUser, a.BillingDateTime, a.PlanRevertDate, a.RevertDate, a.ResourceStatus, "
             + "   a.Driver, a.Editor, a.EditTime, a.ValidMark, a.Spare1, a.Spare2, a.Spare3, a.Spare4,a.LoanNumber,a.Reverter, b.ResourceName, b.ResourceCode,  EditorTable.UserName AS EditorName ,"
             + "  b.ResourceModel, b.ResourceType, b.CurrentStatus, b.Memo,ApplyUserTable.PersonName AS ApplyUserName,edp.DepName ,sds.keyvalue as resourcetypename , OperationTable.UserName AS OperatorName,  ReverterTable.PersonName AS ReverterName "
             + "  FROM    dbo.RepResourceLoan AS a LEFT OUTER JOIN dbo.EmpPersonBase AS ApplyUserTable ON a.ApplyUser = CONVERT(varchar(10), ApplyUserTable.PersonID) LEFT OUTER JOIN "
             + "  dbo.EmpDep AS edp ON a.UseDepID = edp.DepID LEFT OUTER JOIN dbo.RepResourceMang AS b ON a.RepResourceMangID = b.RepResourceMangID   left join dbo.SysDictionarySub as sds  on b.resourcetype=sds.id and sds.mainkeyid=14 ");
            strSql.Append("  LEFT OUTER JOIN  dbo.SysUser AS OperationTable ON a.Operator = OperationTable.UserID   LEFT OUTER JOIN  dbo.SysUser AS EditorTable ON a.Editor = EditorTable.UserID   LEFT OUTER JOIN  dbo.EmpPersonBase AS ReverterTable ON a.Reverter=ReverterTable.PersonID ");
            strSql.Append(" where a.ValidMark=1 and a.RepResourceLoanID=@RepResourceLoanID "+strCondtion+" order by RepResourceLoanID desc ");
            SqlParameter[] parameters = {
					new SqlParameter("@RepResourceLoanID", SqlDbType.Int,4)
};
            parameters[0].Value = RepResourceLoanID;
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            return ds;
        }
        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "RepResourceLoan";
            parameters[1].Value = "RepResourceLoanID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  Method
    }
}

